package com.yilin.platform.service.impl.capital;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yilin.platform.base_core.application.PageData;
import com.yilin.platform.base_core.commons.utils.DateTimeUtil;
import com.yilin.platform.base_core.commons.utils.MathUtil;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.commons.utils.StringUtil;
import com.yilin.platform.base_core.tools.QueryUtil;
import com.yilin.platform.entity.capital.AdvanceMoney;
import com.yilin.platform.entity.truck.TruckVehicle;
import com.yilin.platform.entity.user.Account;
import com.yilin.platform.entity.user.Organize;
import com.yilin.platform.mapper.capital.AdvanceMoneyMapper;
import com.yilin.platform.service.capital.IAdvanceMoneyService;
import com.yilin.platform.service.impl.truck.TruckVehicleService;
import com.yilin.platform.service.impl.user.AccountService;
import com.yilin.platform.service.impl.user.OrganizeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class AdvanceMoneyService extends ServiceImpl<AdvanceMoneyMapper, AdvanceMoney> implements IAdvanceMoneyService {
    @Resource
    private AdvanceMoneyMapper advanceMoneyMapper;

    @Resource
    private TruckVehicleService truckVehicleService;
    @Resource
    private AccountService accountService;
    @Resource
    private OrganizeService organizeService;
    //添加或者修改支出记录
    @Override
    public void saveOrUpdateAdvanceMoneySubtract(Account account, AdvanceMoney advanceMoney, TruckVehicle truckVehicle) {
        account=accountService.getById(account.getId());
        advanceMoney.setAccountId(account.getId());
        advanceMoney.setAccountName(account.getName());
        advanceMoney.setOrganizeId(account.getOrganizeId());
        advanceMoney.setAdvanceState(AdvanceMoney.AdvanceState.audting);
        advanceMoney.setAdvanceType(AdvanceMoney.AdvanceType.expenditure);
        advanceMoney.setOrganizeName(account.getOrganizeName());
        if(StringUtil.isNotEmpty(advanceMoney.getSuperiorPlatId())){
            Organize organize=organizeService.getOrganizeById(advanceMoney.getSuperiorPlatId());
            ParamUtil.isNull(organize,"获取合作组织失败");
            advanceMoney.setSuperiorPlatId(organize.getId());
            advanceMoney.setSuperiorPlatName(organize.getOrganizeName());
        }else{
            advanceMoney.setSuperiorPlatId(account.getSuperiorPlatId());
            advanceMoney.setSuperiorPlatName(account.getSuperiorPlatName());
        }
        if (advanceMoney.getTradeDate() == null) advanceMoney.setTradeDate(new Date());
        if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.integral) {
            advanceMoney.setTruckNo(truckVehicle.getTrackNo());
            advanceMoney.setTruckId(truckVehicle.getId());
            if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle) {
                truckVehicle.setAdvanceMoney(MathUtil.subtract(truckVehicle.getAdvanceMoney(), advanceMoney.getMonery()));
            } else if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill) {
                truckVehicle.setOillMoney(MathUtil.subtract(truckVehicle.getOillMoney(), advanceMoney.getMonery()));
            } else if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.integral) {
                truckVehicle.setOillIntegral(MathUtil.subtract(truckVehicle.getOillIntegral(), advanceMoney.getMonery()));
            }
            truckVehicleService.updateById(truckVehicle);
        } else {
            account.setMonery(MathUtil.subtract(account.getMonery(), advanceMoney.getMonery()));
            accountService.updateById(account);
        }
        advanceMoney.setAdvanceNo(DateTimeUtil.dateNo("ZC"));
        saveOrUpdate(advanceMoney);
    }

    //添加或者收入
    @Override
    public void saveOrUpdateAdvanceMoneyAdd(Account loginAccount, String accountId, AdvanceMoney advanceMoney, TruckVehicle truckVehicle,Boolean negative) {
        loginAccount=accountService.getById(loginAccount.getId());
        if(negative!=null&&negative)advanceMoney.setMonery(advanceMoney.getMonery()*-1);
        advanceMoney.setAdvanceState(AdvanceMoney.AdvanceState.success);
        advanceMoney.setAdvanceType(AdvanceMoney.AdvanceType.income);
        advanceMoney.setAuditingId(loginAccount.getId());
        advanceMoney.setAuditingName(loginAccount.getName());
        advanceMoney.setAuditingDate(new Date());
        advanceMoney.setRechargeId(loginAccount.getId());
        advanceMoney.setRechargeName(loginAccount.getName());
        advanceMoney.setRechargeOrganizeId(loginAccount.getOrganizeId());
        advanceMoney.setRechargeOrganizeName(loginAccount.getOrganizeName());
        if (advanceMoney.getTradeDate() == null) advanceMoney.setTradeDate(new Date());
        if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.integral) {
            advanceMoney.setAccountId(truckVehicle.getManageUserId());
            advanceMoney.setAccountName(truckVehicle.getManageUserName());
            advanceMoney.setTruckId(truckVehicle.getId());
            advanceMoney.setTruckNo(truckVehicle.getTrackNo());
            advanceMoney.setOrganizeId(truckVehicle.getOrganizeId());
            advanceMoney.setOrganizeName(truckVehicle.getOrganizeName());
            advanceMoney.setSuperiorPlatId(truckVehicle.getSuperiorPlatId());
            advanceMoney.setSuperiorPlatName(truckVehicle.getSuperiorPlatName());
            if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle) {
                truckVehicle.setAdvanceMoney(MathUtil.add(truckVehicle.getAdvanceMoney(), advanceMoney.getMonery()));
            } else if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill) {
                truckVehicle.setOillMoney(MathUtil.add(truckVehicle.getOillMoney(), advanceMoney.getMonery()));
            } else if (advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.integral) {
                truckVehicle.setOillIntegral(MathUtil.add(truckVehicle.getOillIntegral(), advanceMoney.getMonery()));
            }
            if(advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.oill || advanceMoney.getExpenseType() == AdvanceMoney.ExpenseType.vehicle ){
                 Account account=accountService.getById(loginAccount.getId());
                 account.setMonery(MathUtil.subtract(account.getMonery(), advanceMoney.getMonery()*-1));
                 accountService.updateById(account);
            }
            truckVehicleService.updateById(truckVehicle);
        } else {
            ParamUtil.isNull(accountId, "用户获取失败");
            Account account = accountService.getAccountById(accountId);
            advanceMoney.setAccountId(account.getId());
            advanceMoney.setAccountName(account.getName());
            advanceMoney.setOrganizeId(account.getOrganizeId());
            advanceMoney.setOrganizeName(account.getOrganizeName());
            advanceMoney.setSuperiorPlatId(account.getSuperiorPlatId());
            advanceMoney.setSuperiorPlatName(account.getSuperiorPlatName());
            ParamUtil.isNull(account, "获取用户失败");
            account.setMonery(MathUtil.add(account.getMonery(), advanceMoney.getMonery()));
            accountService.updateById(account);
        }
        advanceMoney.setAdvanceNo(DateTimeUtil.dateNo("SR"));
        saveOrUpdate(advanceMoney);
    }

    @Override
    public Map<String, Object> getEmpAccountStatic(String accountId, String startDate, String endDate) {
        String year=DateTimeUtil.getYear();
        String month=DateTimeUtil.getMonth();
        if(StringUtil.isEmpty(startDate)) startDate=year+"-"+month+"-01";
        if(StringUtil.isEmpty(endDate))endDate=year+"-"+month+"-31";
        Map<String, Object>  capitalMap= advanceMoneyMapper.getEmpAccountStatic(accountId,startDate,endDate);
        if(capitalMap==null)capitalMap=new HashMap<>();
        Account account1=accountService.getAccountById(accountId);
        capitalMap.put("endMoney",account1.getMonery());
        return  capitalMap;
    }


    //审核
    @Override
    public void auditingAdvanceMoney(Account account, AdvanceMoney advanceMoney) {
        advanceMoney.setAuditingId(account.getId());
        advanceMoney.setAuditingName(account.getName());
        advanceMoney.setAuditingDate(new Date());
        saveOrUpdate(advanceMoney);
    }

    //根据ID查询
    @Override
    public AdvanceMoney getAdvanceMoneyById(String id) {
        return advanceMoneyMapper.selectById(id);
    }

    /**
     * 获取分页
     */
    @Override
    public PageData<AdvanceMoney> getAdvanceMoneyPage(Account loginUser, AdvanceMoney query, Integer start, Integer limit) {
        QueryWrapper<AdvanceMoney> queryWrapper = new QueryWrapper<>();
        QueryUtil.buildOrganizeIdOrSuperiorPlatIdWrapper(queryWrapper, loginUser);
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return page(new PageData<>(start, limit), queryWrapper);
    }
    @Override
    public PageData<AdvanceMoney> getAdvanceMoneyByAccountPage( AdvanceMoney query, Integer start, Integer limit) {
        QueryWrapper<AdvanceMoney> queryWrapper = new QueryWrapper<>();
        if(query.getExpenseType()== AdvanceMoney.ExpenseType.vehicle){
            queryWrapper.eq("expense_type",AdvanceMoney.ExpenseType.vehicle.index);
            queryWrapper.eq("advance_type",AdvanceMoney.AdvanceType.income.index);
            queryWrapper.eq("recharge_id",query.getAccountId());
        }else if(query.getExpenseType()== AdvanceMoney.ExpenseType.oill){
            queryWrapper.eq("expense_type",AdvanceMoney.ExpenseType.oill.index);
            queryWrapper.eq("advance_type",AdvanceMoney.AdvanceType.income.index);
            queryWrapper.eq("recharge_id",query.getAccountId());
        }else if(query.getExpenseType()== AdvanceMoney.ExpenseType.travel||query.getExpenseType()== AdvanceMoney.ExpenseType.office){
            queryWrapper.in("expense_type",AdvanceMoney.ExpenseType.travel.index,AdvanceMoney.ExpenseType.office.index);
            queryWrapper.eq("advance_type",AdvanceMoney.AdvanceType.expenditure.index);
            queryWrapper.eq("account_id",query.getAccountId());
        }else {
            queryWrapper.in("expense_type",AdvanceMoney.ExpenseType.oill.index,AdvanceMoney.ExpenseType.vehicle.index,AdvanceMoney.ExpenseType.travel.index,AdvanceMoney.ExpenseType.office.index);
            queryWrapper.and(wrapper ->
                    wrapper.eq("account_id", query.getAccountId())
                            .or()
                            .eq("recharge_id", query.getAccountId())
            );
        }
        QueryUtil.andQueryWrapper(queryWrapper, query);
        return page(new PageData<>(start, limit), queryWrapper);
    }

    @Override
    public List<Map<String,Object>> getAdvanceMoneyPage(String[] truckIds) {
        return advanceMoneyMapper.getVeclickAdvaceMap(Arrays.asList(truckIds), DateTimeUtil.getYear(), DateTimeUtil.getMonth(),DateTimeUtil.getDay() );
    }
    @Override
    public Map<String,Object> getOrganizeAdvaceStatic(String organizeId) {
        return advanceMoneyMapper.getOrganizeAdvaceStatic(organizeId);
    }



}
